package cordova.plugin.chinesetopinyin;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import android.util.Log;


public class ChineseToPinYin extends CordovaPlugin{
	public Pinyin(){}
	public String winParam;
	CallbackContext callbackContext;
	@Override
	public boolean execute(String action, JSONArray args,
            CallbackContext callbackContext) throws JSONException {
            this.callbackContext = callbackContext;
            Log.i("jiang", "action is"+action);
            String message = args.getString(0);
            Log.i("jiang", message);
            winParam = this.getPinYin(message);
            callbackContext.success(winParam);
			return true;
			}


	public static String getPinYin(String inputString){
		Log.i("jiang", "1111");
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

        char[] input = inputString.trim().toCharArray();
        String output="";

        try {
            for(int i = 0; i < input.length; i++){
                if(java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")){
                        String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i],format);
                        output+=temp[0];
                }else{
                    output+=input[i];
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("jiang", output);
        return output;
    }



}